<template>
    <el-dialog :visible.sync="selectDialogueVisible" width="680px" :before-close="cancel" class="addQuestion" destroy-on-close>
        <div slot="title" class="dialog-title">
            <span>选择会话</span>
        </div>
        <div>
            <el-table v-loading="listLoading" :data="list" border fit style="width: 100%">
                <el-table-column label="会话名称">
                    <template slot-scope="scope">
                        <span>{{ scope.row.dialogueName }}</span><br />
                        <span v-for="(intention, key) in scope.row.intentionList" :key="key">
                            <el-radio v-model="chosenIntention" @change="select"
                                      :label="intention.intentionName + '@' + scope.row.dialogueId + ',' + intention.id">
                                {{ intention.intentionName }}</el-radio>
                        </span>
                    </template>
                </el-table-column>

                <!-- <el-table-column align="center" label="操作" width="180" fixed="right">
                    <template slot-scope="scope">
                        <el-button @click="select" size="mini" type="primary">选择</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageNo" :limit.sync="pageSize" @pagination="getList" />
        </div>
    </el-dialog>

</template>

<script>
import Pagination from "@/components/Pagination";
import { getDialogueList } from "@/api/knowledge";
export default {
    name: "selectDialogue",
    props: {
        selectDialogueVisible: Boolean,
    },
    data() {
        return {
            chosenIntention: "",
            listLoading: false,
            list: [],
            pageNo: 1,
            pageSize: 10,
            total: 0,
        };
    },
    watch: {
        selectDialogueVisible(val) {
            if (val && this.list.length == 0) {
                this.getList()
            }
        },
    },
    methods: {
        changePageSize(val) {
            this.pageSize = val;
            this.getList();
        },
        changePageNo(val) {
            this.pageNo = val;
            this.getList();
        },

        getList() {
            this.listLoading = true;
            getDialogueList({}).then((resp) => {
                console.log('getDialogueList resp', resp)
                this.list = resp.data
                this.listLoading = false;
                this.total = resp.data.total;
                this.list = this.extractDialogueIntentionList(resp.data.dialogueList);
            });
        },
        extractDialogueIntentionList(dialogueList) {
            for (var m = 0; m < dialogueList.length; m++) {
                var dialogue = dialogueList[m];
                var intentionList = [];
                if (
                    dialogue.dialogueProcess &&
                    dialogue.dialogueProcess.hasOwnProperty("nodes")
                ) {
                    for (var n = 0; n < dialogue.dialogueProcess.nodes.length; n++) {
                        var node = dialogue.dialogueProcess.nodes[n];
                        var intentionItem = {};
                        if (node.data.form.nodeType == "意图入口") {
                            intentionItem["id"] = node.id;
                            intentionItem["intentionName"] = node.data.form.intentionName;
                            intentionList.push(intentionItem);
                        }
                    }
                }
                dialogueList[m].intentionList = intentionList;
            }
            return dialogueList;
        },
        select() {
            // if (this.chosenIntention == '') {
            //     this.$antMessage.warning("请选择会话");
            //     return
            // }
            var data = {
                'modeValue': this.chosenIntention.split('@')[1],
                'dialogueName': this.chosenIntention.split('@')[0],
            }
            console.log('chooseDialogue data', data)
            this.$emit("chooseDialogue", data);
            this.cancel()
        },
        cancel() {
            this.$emit("update:selectDialogueVisible", false);
        },
    },
    computed: {

    },
    components: { Pagination },
};
</script>

<style lang="scss">
</style>